package com.codingfuture.glacierdataapi.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.codingfuture.glaciercommon.util.EntityConverter;
import com.codingfuture.glacierdataapi.dto.AlarmProcessRecordDTO;
import com.codingfuture.glacierdataapi.entity.AlarmRecord;
import com.codingfuture.glacierdataapi.vo.AlarmModeVo;
import com.codingfuture.glaciercommon.entity.Warehouse;
import com.codingfuture.glacierdataapi.service.DataApiService;
import com.codingfuture.glaciercommon.entity.MeterRecord;
import com.codingfuture.glaciercommon.util.PageDate;
import com.codingfuture.glaciercommon.util.PageParam;
import com.codingfuture.glaciercommon.util.Result;
import com.codingfuture.glacierdataapi.vo.AlarmRecordVO;
import com.codingfuture.glacierdataapi.vo.WarehouseAndMeterVO;
import com.codingfuture.glacierdataapi.vo.WarehouseDataVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: Ji
 * @DateTime: 2025-04-09 15:41
 * @Description:
 **/
@RestController
@Slf4j
@Api(tags = "DATAAPI")
@RefreshScope
//@RequestMapping("/glacier-dataApi")
public class DataApiController {
    @Autowired
    private DataApiService dataApiService;

    /**
     * 此接口未完善，后端数据库表格不全，alarm_number不确定
     * 暂时用warehouse实体类测试
     *
     * @param ids
     * @param companyId
     * @return
     */
    @Cacheable("warehouse")
    @GetMapping("/monitoringWarehouse/getWarehouse")
    public Result<List<WarehouseDataVO>> getWarehouse(@RequestParam(name = "ids", required = false) Long ids,
                                                      @RequestParam(name = "companyId", required = false) Long companyId) {
        List<WarehouseDataVO> WarehouseVOS = dataApiService.getWarehouse(ids, companyId);
        log.debug("warehouse:{}", WarehouseVOS);
//        PageDate<List<WarehouseDataVO>> pageDate = new PageDate<>();
//        log.debug("pageDate:{}", pageDate);
//        pageDate.setRows(WarehouseVOS);
//        pageDate.setTotal((long) WarehouseVOS.size());
        return Result.successes(WarehouseVOS);
    }

    /**
     * 实体类为什么？？对应数据库那个表？？redis和MySQL都要对应相关表格
     * 暂时用meterRecord实体类做测试
     *
     * @param pageParam
     * @param companyId
     * @param statusesId
     * @return
     */
    @GetMapping("/meterRecord/getMeterRecord")
    public Result<PageDate<List<WarehouseAndMeterVO>>> getMeterRecord(PageParam pageParam,
                                                                      @RequestParam(name = "companyId", required = false) Long companyId,
                                                                      @RequestParam(name = "statusesId") Long statusesId) {
//        Page<MeterRecord> page = new Page<>(pageParam.getPageNum(), pageParam.getPageSize());
        List<WarehouseAndMeterVO> warehouseAndMeterVOS=dataApiService.getMeterRecord(pageParam, companyId, statusesId);
        PageDate<List<WarehouseAndMeterVO>> pageDate = new PageDate<>();
        pageDate.setRows(warehouseAndMeterVOS);
        pageDate.setTotal((long) warehouseAndMeterVOS.size());
        return Result.OK(pageDate);
    }

    @GetMapping("/AlarmProcessMode/getMode")
    public Result<List<AlarmModeVo>> getMode() {
        List<AlarmModeVo> alarmModeVos = dataApiService.getMode();
        PageDate<List<AlarmModeVo>> pageDate = new PageDate<>();
        pageDate.setRows(alarmModeVos);
        pageDate.setTotal((long) alarmModeVos.size());
        return Result.OK(alarmModeVos);
    }


    @PostMapping("/AlarmProcessRecord/add")
    public Result<Void> add(AlarmProcessRecordDTO alarmProcessRecordDTO) {
        return dataApiService.add(alarmProcessRecordDTO) ?
                Result.CZSuccess() : Result.err();
    }

    @GetMapping("/alarmRecord/getPage")
    public Result<PageDate<List<AlarmRecordVO>>> getPage(PageParam pageParam,
                                                         @RequestParam(name = "warehouseSn", required = false) String warehouseSn,
                                                         @RequestParam(name = "startTime", required = false) LocalDateTime startTime,
                                                         @RequestParam(name = "endTime", required = false) LocalDateTime endTime) {
        Page<AlarmRecord> page = new Page<>(pageParam.getPageNum(), pageParam.getPageSize());
        dataApiService.getPage(page, warehouseSn, startTime, endTime);
        PageDate<List<AlarmRecordVO>> pageDate = new PageDate<>();
        List<AlarmRecordVO> alarmRecordVOS = new ArrayList<>();
        List<AlarmRecord> alarmRecordS = page.getRecords();
        for (AlarmRecord alarmRecord : alarmRecordS) {
            AlarmRecordVO alarmRecordVO = EntityConverter.entity2DTO(alarmRecord, AlarmRecordVO.class);
            alarmRecordVOS.add(alarmRecordVO);
        }
        pageDate.setRows(alarmRecordVOS);
        Long total = dataApiService.count(warehouseSn, startTime, endTime);
        pageDate.setTotal(total);
        log.debug("pageDate:{}", pageDate);
        return Result.CZsuccess(pageDate);
    }


}
